/*
Jason Curtis 2011

module built on OneWire.h to simplify temp acquisition from ds18b20
temperature sensors.

Uses code from www.hacktronics.com LCD thermostat project
*/
#define ONEWIRE_SEARCH 1
#include "OneWireTemp.h"

OWTemp::OWTemp(uint8_t pin) //init function
    :OneWire(pin) //call parent class initializer
{
}

// get updated temperatures, in F and C, from the sensor.
// returns 1 if successful, 0 if not.
uint8_t OWTemp::getTemp(){
    int foo, bar;

    reset();
    select(addr);
    write(0x44,1);

    reset();
    select(addr);    
    write(0xBE);

    for ( i = 0; i < 9; i++) {
    data[i] = read();
    }

    LowByte = data[0];
    HighByte = data[1];
    TReading = (HighByte << 8) + LowByte;
    SignBit = TReading & 0x8000;  // test most sig bit

    if (SignBit) {
    TReading = -TReading;
    }
    Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25
    Whole = Tc_100 / 100;          // separate off the whole and fractional portions
    Fract = Tc_100 % 100;
    if (Fract > 49) {
    if (SignBit) {
      --Whole;
    } else {
      ++Whole;
    }
    }

    if (SignBit) {
    bar = -1;
    } else {
    bar = 1;
    }
    foo = ((Whole * bar) * 18);      // celsius to fahrenheit conversion section
    FWhole = (((Whole * bar) * 18) / 10) + 32;
    if ((foo % 10) > 4) {            // round up if needed
       ++FWhole;
    }
}